package com.jiyun.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.jiyun.model.*;
import com.jiyun.service.CourseService;
import com.jiyun.service.PaperUserService;
import com.jiyun.service.RaceService;
import com.jiyun.service.RaceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lenovo
 */
@RestController
@RequestMapping("index")
public class IndexController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RaceService raceService;

    @Autowired
    private RaceUserService raceUserService;

    @Autowired
    private PaperUserService paperUserService;

    @RequestMapping("count")
    public List<Long> count(@RequestParam String token) {
        Object o = redisTemplate.opsForValue().get(token);
        String s = JSONObject.toJSONString(o);
        String o1 = JSONObject.parse(s).toString();
        User user = JSONObject.parseObject(o1, User.class);
        return courseService.showCount(user.getId());
    }

    @RequestMapping("courseList")
    public List<Course> courseList() {
        return courseService.courseList();
    }

    @RequestMapping("dian")
    public void dian(@RequestParam Integer id) {
        Object o = redisTemplate.boundValueOps(String.valueOf(id)).get();
        Course course = courseService.getById(id);
        if (o == null) {
            course.setGood(course.getGood() + 1);
            courseService.updateById(course);
            redisTemplate.boundValueOps(String.valueOf(id)).set(JSONObject.toJSONString(course.getGood()));
        } else {
            course.setGood(course.getGood() - 1);
            courseService.updateById(course);
            redisTemplate.delete(String.valueOf(id));
        }
    }

    @RequestMapping("courseListD")
    public List<Course> courseListD() {
        return courseService.courseListD();
    }

    @RequestMapping("trainListD")
    public List<Train> trainList() {
        return courseService.trainList();
    }

    @RequestMapping("quizList")
    public List<Quiz> quizList() {
        return courseService.quizList();
    }

    @RequestMapping("courseFindPage")
    public Page<Course> courseFindPage(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestBody Course course) {
//        String s = JSONObject.toJSONString(course.getLids());
//        String s2=s.replace("[","");
//        String s3=s2.replace("]","");
//        course.setLid(s3);
        return courseService.findPage(page, pageSize, course);
    }

    @RequestMapping("raceFindPage")
    public PageInfo<Race> RaceFindPage(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestBody Race race) {
//        String s = JSONObject.toJSONString(course.getLids());
//        String s2=s.replace("[","");
//        String s3=s2.replace("]","");
//        course.setLid(s3);
        return courseService.raceFindPage(page, pageSize, race);
    }

    @RequestMapping("labelFindAll")
    public List<Label> labelFindAll() {
        return courseService.labelFindAll();
    }

    @RequestMapping("courseId")
    public Course courseId(@RequestParam Integer id) {
        Course course = courseService.getById(id);
        Object o = redisTemplate.boundValueOps(course.getId() + "").get();
        if (o == null) {
            course.setTyped(0);
        } else {
            course.setTyped(1);
        }
        return course;
    }
    @RequestMapping("raceId")
    public Race raceId(@RequestParam Integer id) {
        Race race= raceService.getById(id);
        return race;
    }

    @RequestMapping("labelName")
    public List<String> labelName(@RequestParam Integer id) {
        List<String> list = new ArrayList<>();
        Course course = courseService.getById(id);
        String[] split = course.getLid().split(",");
        for (int i = 0; i < split.length; i++) {
            String a = courseService.selectLable(Integer.valueOf(split[i]));
            list.add(a);
        }
        return list;
    }

    @RequestMapping("labelName2")
    public List<String> labelName2(@RequestParam Integer id) {
        List<String> list = new ArrayList<>();
        Race race = raceService.getById(id);
        String[] split = race.getLid().split(",");
        for (int i = 0; i < split.length; i++) {
            String a = raceService.selectLable(Integer.valueOf(split[i]));
            list.add(a);
        }
        return list;
    }

    @RequestMapping("catalog")
    public List<Catalog> catalog(@RequestParam Integer id,@RequestParam Integer ids) {
        return courseService.catalog(id,ids);
    }

    @RequestMapping("cataloG")
    public List<Catalog> cataloG(@RequestParam Integer id,@RequestParam Integer ids,@RequestParam Integer idss) {
        return courseService.cataloG(id,ids,idss);
    }

    @RequestMapping("toJinDu")
    public Integer toJinDu(@RequestParam Integer id,@RequestParam Integer ids) {
        return courseService.toJinDu(id,ids);
    }

    @RequestMapping("toBao")
    public void toBao(@RequestParam Integer id,@RequestParam Integer uid,@RequestParam Integer tid) {
        raceService.toBao(id,uid,tid);
    }

    @RequestMapping("Bao")
    public RaceUser toBao(@RequestParam Integer id,@RequestParam Integer uid) {
       return courseService.Bao(id,uid);
    }

    @RequestMapping("findTeam")
    public List<Team> findTeam(){
       return courseService.findTeam();
    }

    @RequestMapping("findContestant")
    public List<String> findContestant(@RequestParam Integer rid,@RequestParam Integer tid){
        return courseService.findContestant(rid,tid);
    }

    @RequestMapping("findQuiz")
    public List<Quiz> findQuiz(@RequestParam Integer id){
        return courseService.findQuiz(id);
    }

    @RequestMapping("findPaper")
    public PaperUser findPaper(@RequestBody PaperUser paperUser){
        return courseService.findPaper(paperUser);
    }

    @RequestMapping("AddPaper")
    public PaperUser AddPaper(@RequestBody PaperUser paperUser){
       PaperUser paperUser1=courseService.findPaper(paperUser);
       if (paperUser1==null){
           paperUserService.save(paperUser);
       }else {
           paperUser1.setInfo(paperUser.getInfo());
           paperUserService.updateById(paperUser1);
       }
        return paperUser;
    }

    @RequestMapping("findPaperPid")
    public List<Integer> findPaperPid(@RequestParam List<Integer> ids,@RequestBody PaperUser paperUser){
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            Integer id = ids.get(i);
            paperUser.setPid(id);
            PaperUser paper = courseService.findPaper(paperUser);
            if (paper == null) {
                Integer a;
                a=i+1;
                list.add(a);
            }
        }
        return list;
    }

    @RequestMapping("findScore")
    public PaperUser findScore(@RequestBody PaperUser paperUser){
        return courseService.findScore(paperUser);
    }
    @RequestMapping("toRankings")
    public List<Rankings> toRankings(@RequestParam Integer qid){
        return courseService.toRankings(qid);
    }


}
